package com.example.chatroom.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;
import java.util.concurrent.TimeUnit;


@Slf4j
@Configuration
public class RedisUtil {
    /**
     * 本项目的redis只存放string数据，所以直接注入这个 对象->JSON字符串
     */
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 设置值（无时间限制）
     * @param key
     * @param value
     * @return
     */
    public boolean set(String key,String value){
        try{
            if(StringUtils.hasLength(key)&&StringUtils.hasLength(value)){
                //不为空才能设置
                stringRedisTemplate.opsForValue().set(key,value);
                return true;
            }
        }catch (Exception e){
            //捕获异常
            log.info("RedisUtil error: set({},{}) ",key,value,e);
            return false;
        }
        //走到这说明捕获到了异常
        return false;
    }

    /**
     * 设置值 有时间限制
     * @param key
     * @param value
     * @param time
     * @return
     */
    public boolean set(String key,String value,Long time){
        log.info("redis set：({},{},{})",key,value,time);
        try{
            if(StringUtils.hasLength(key)&&StringUtils.hasLength(value)){
                //不为空才能设置
                stringRedisTemplate.opsForValue().set(key,value,time, TimeUnit.SECONDS);
                return true;
            }
        }catch (Exception e){
            //捕获异常
            log.info("RedisUtil error: set({},{},{}) ",key,value,time,e);
            return false;
        }
        return false;
    }

    /**
     * 获取值
     * @param key
     * @return
     */
    public String get(String key){
        try{
            return StringUtils.hasLength(key)
                    ?stringRedisTemplate.opsForValue().get(key)
                    :null;
        }catch (Exception e){
            //捕获异常
            log.info("RedisUtil error : get({}) ",key,e);
            return null;
        }
    }

    /**
     * 删除值
     * @param key
     * @return
     */
    public boolean delete(String... key){
        try{
            if(key!=null&&key.length>0){
                if(key.length==1){
                    stringRedisTemplate.delete(key[0]);
                }else{
                    //key有多个
                    stringRedisTemplate.delete(
                            (Collection<String>) CollectionUtils.arrayToList(key)
                    );
                }
            }
            return true;
        }catch (Exception e){
            log.info("RedisUtil error : delete({}) ",key,e);
            return false;
        }
    }

    /**
     * 判断key是否存在
     * @param key
     * @return
     */
    public boolean hasKey(String key){
        try{
            return StringUtils.hasLength(key)
                    ?stringRedisTemplate.hasKey(key)
                    :false;
        }catch (Exception e){
            log.info("RedisUtil error : hasKey({})",key,e);
            return false;
        }
    }
}
